package com.lgy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgy.dao.EmployeeDao;
import com.lgy.pojo.Employee;
import com.lgy.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeDao employeeDao;
    @Override
    public int deleteByPrimaryKey(Integer empid) {
        return employeeDao.deleteByPrimaryKey(empid);
    }

    @Override
    public int insert(Employee record) {
        //根据公司id查询数据库,确定当前提交的姓名是否重复
        List<Employee> emps = employeeDao.findEmpByCompanyId(record.getCompanyid());
        for (Employee employee : emps) {
            if(employee.getEmpphone().equals(record.getEmpphone())){
                return 0;
            }
        }
        return employeeDao.insert(record);
    }

    @Override
    public int insertSelective(Employee record) {
        return employeeDao.insertSelective(record);
    }

    @Override
    public Employee selectByPrimaryKey(Integer empid) {
        return employeeDao.selectByPrimaryKey(empid);
    }

    @Override
    public int updateByPrimaryKeySelective(Employee record) {
        return employeeDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Employee record) {
        return employeeDao.updateByPrimaryKey(record);
    }

    @Override
    public Boolean findByPhoneNumber(String empPhoneNumber) {
        Employee employee = employeeDao.findByPhoneNumber(empPhoneNumber);
        if(employee!=null){
            return true;
        }else return false;
    }

    @Override
    public Boolean findByEmpName(String empname) {
        List<Employee> employee = employeeDao.findByEmpName(empname);
        if(employee.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean findEmpByCompanyId(Integer companyid, String empphone) {
        List<Employee> list = employeeDao.findEmpByCompanyId(companyid);

        for (Employee employee : list) {
            if (empphone.equals(employee.getEmpphone())) {
                return true;
            }
        }return false;
    }

    @Override
    public Employee findEmp(Employee employee) {
        List<Employee> list = employeeDao.findEmpByCompanyId(employee.getCompanyid());
        if(list!=null&&list.size()>0){
            for (Employee emp : list) {
                if(emp.getPositionid()==employee.getPositionid()&&emp.getEmpname().equals(employee.getEmpname())
                        &&emp.getEmpphone().equals(employee.getEmpphone())&&emp.getEmppassword().equals(employee.getEmppassword())){
                    return emp;
                }
            }
        }
        return null;
    }

    @Override
    public PageInfo<Employee> findEmpAndPositionName(Integer companyId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(employeeDao.findEmpAndPositionName(companyId));
        return pageInfo;
    }

    @Override
    public Employee findByEmpId(int empId) {
        return employeeDao.findByEmpId(empId);
    }

    @Override
    public List<Employee> getAllEmpByCompanyId(Integer companyId) {
        return employeeDao.getAllEmpByCompanyId(companyId);
    }


}
